const app = getApp();
Page({
    data: {
        userName: '',
        password: '',
    },
    onload: function () {
        wx.setNavigationBarTitle({
            title: ' '
        })
    },
    userName: function (e) {
        this.setData({
            userName: e.detail.value
        });
    },
    password: function (e) {
        this.setData({
            password: e.detail.value
        });
    },
    login: function () {
        console.log(1);
        var userName = this.data.userName;
        var password = this.data.password;
        if (userName == '') {
            wx.showModal({
                content: '请输入用户名',
                showCancel: false,
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击确定')
                    }
                }
            })
        } else if (password == '') {
            wx.showModal({
                content: '请输入密码',
                showCancel: false,
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击确定')
                    }
                }
            })
        } else {
            wx.login({
                success: res => {
                    if(res.code){
                        wx.showLoading({
                            title: '登录中，请稍侯',
                        });

                    //获取code后登录
                    wx.request({
                        url: app.globalData.APIURL + '/vending-admin/admin/login/index',
                        data: {
                            uuid:'miniprogram',
                            username: userName,
                            password: password,
                            code: res.code,
                            type:1,
                        },
                        // method:'GET',
                        header: {
                            'content-type': 'application/json' // 默认值
                        },
                        success: function (res) {
                            wx.hideLoading();
                            console.log(res.data);
                            if(app.requestEro(res.data.success,res.data.msg)){
                                app.globalData.token = res.data.token;
                                app.globalData.userInfo = res.data.user;
                                wx.setStorageSync('userInfo', res.data.user);
                                wx.setStorageSync('token', res.data.token);

                                app.gohome();
                            }

                        },
                        fail: function (err) {
                            wx.hideLoading();
                            console.log(err)
                        }
                    })
                    }else{
                        console.log('登录失败！' + res.errMsg)
                    }
                }
            })

        }
    },


});